package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.WarehousingCheck;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IWarehousingService;
import com.woniu.supermarket.util.CodeCreateUtil;
import com.woniu.supermarket.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("warehouse")
@Api(value="商品入库操作")
public class WarehousingController {
    @Autowired
    IWarehousingService warehousingService;

    @GetMapping("/wlist")
    @ApiOperation(value = "分页查询入库操作")
    public ResultResponse<PageInfo<WarehousingVo>> wlist(@ApiParam(name="now",value = "当前页")@RequestParam(name = "now",defaultValue = "1") Integer now,
                                                         @ApiParam(name="size",value = "页大小")@RequestParam(name = "size",defaultValue = "3") Integer size,
                                                         String warehousingNumber,String status){
        ResultResponse<PageInfo<WarehousingVo>> result = null;
        try {
            PageInfo<WarehousingVo> pageInfo = warehousingService.findWarehousingByCons(now, size, warehousingNumber,status);
            result = new ResultResponse<>(200,"查询入库单成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询入库单失败");
        }
        return result;

    }


    @PostMapping("/add")
    @ApiOperation(value = "添加入库单操作")
    public ResultResponse<WarehousingVo> addWarehousing(@ApiParam(value = "接收入库单对象")@RequestBody WarehousingVo warehousingVo){
        ResultResponse<WarehousingVo> result = null;

        try {
            int i  = warehousingService.addWarehousingDetail(warehousingVo);
            if(i>0){
                result = new ResultResponse<>(200,"添加商品入库单成功",warehousingVo);
            }else{
                result = new ResultResponse<>(201,"添加商品入库单失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(555,"系统错误");
        }
        return result;
    }


        @PutMapping("/modify")
        @ApiOperation(value = "修改订单操作")
        public ResultResponse<Void> modifyWarehousing(@RequestBody WarehousingVo warehousingVo) {
            ResultResponse<Void> result = null;
            try {
                warehousingService.modifyWarehousing(warehousingVo);
                result = new ResultResponse<>(200, "修改入库单成功");
            } catch (RuntimeException e) {
                e.printStackTrace();
                result = new ResultResponse<>(201, "修改入库单失败");
            }
            return result;
        }


        @DeleteMapping("/del/{id}")
        @ApiOperation(value = "删除入库单操作")
        public ResultResponse<Void> delWarehousing(@PathVariable("id")Integer id) {
            ResultResponse<Void> result = null;
            try {
                warehousingService.removeWarehousing(id);
                result = new ResultResponse<>(200, "删除入库单成功");
            } catch (RuntimeException e) {
                e.printStackTrace();
                result = new ResultResponse<>(201, "删除入库单失败");
            }
            return result;
        }

        @DeleteMapping("/delBatch")
        @ApiOperation(value = "批量删除入库单操作")
        public ResultResponse<Void> delBatch(@RequestParam("ids")int[] ids) {
            ResultResponse<Void> result = null;
            try {
                warehousingService.removeBatchIds(ids);
                result = new ResultResponse<>(200, "批量删除订单成功");
            } catch (RuntimeException e) {
                e.printStackTrace();
                result = new ResultResponse<>(201, "批量删除订单失败");
            }
            return result;
        }
        @GetMapping("/addcode")
        public ResultResponse<String> addcode() {
            ResultResponse<String> result = null;
            try {
                String code = CodeCreateUtil.createWarehousingCode();
                result = new ResultResponse<>(200, "生成编码成功！",code);
            } catch (RuntimeException e) {
                e.printStackTrace();
                result = new ResultResponse<>(201, "生成编码失败！");
            }
            return result;
        }

    @GetMapping("/showAllWarehousing")
    @ApiOperation(value = "查询所有采购单号")
    public ResultResponse<List<WarehousingVo>> showAllPurchase(){
        ResultResponse<List<WarehousingVo>> result = null;
        try {
            List<WarehousingVo> all = warehousingService.findAll();
            result = new ResultResponse<>(200,"查询采购单号成功",all);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询采购单号失败");
        }
        return result;
    }

    @GetMapping("/showWarehousingAndDetailOfOne/{warehousingNumber}")
    @ApiOperation(value = "查询某个入库以及详情，根据采购订单号")
    public ResultResponse<WarehousingVo> showPurchaseAndDetailOfOne(@ApiParam(value = "采购单单号")@PathVariable String warehousingNumber){
        ResultResponse<WarehousingVo> result = null;
        try {
            WarehousingVo warehousingVo = warehousingService.findWarehousingAndDetailByWarehousingNumber(warehousingNumber);
            if(warehousingVo != null){
                result = new ResultResponse<>(200,"查询入库成功",warehousingVo);
            }else{
                result = new ResultResponse<>(200,"查询入库失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(555,"系统错误");
        }
        return result;
    }

    @PostMapping("/auditWarehousing")
    @ApiOperation(value = "审核入库操作")
    public ResultResponse<WarehousingCheck> auditWarehousing(@ApiParam(value = "审核信息对象")@RequestBody WarehousingCheck warehousingCheck){
        ResultResponse<WarehousingCheck> result = null;

        try {
            int i  = warehousingService.auditWarehousingcheck(warehousingCheck);
            if(i>0){
                result = new ResultResponse<>(200,"审核采购订单成功",warehousingCheck);
            }else{
                result = new ResultResponse<>(201,"审核采购订单失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(555,"系统错误");
        }
        return result;
    }
    @GetMapping("/findCount")
    @ApiOperation(value = "查询符合条件的条数")
    public ResultResponse<Integer> findCount(){
        ResultResponse<Integer> result = null;
        try {
           int num = warehousingService.findCount();
            result = new ResultResponse<>(200,"查询条数成功",num);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询条数失败");
        }
        return result;
    }
    //导出
    /*@GetMapping("/exit")
    public ResultResponse<Integer> wexit(){
        ResultResponse<Integer> result = null;
        return result;
    }*/
    @PostMapping("/advancedSearch")
    @ApiOperation(value= "高级搜索")
    public ResultResponse<PageInfo<WarehousingVo>> advancedSearch(@RequestBody AdvancedWarehousingVo advancedWarehousingVo){
        ResultResponse<PageInfo<WarehousingVo>> result = null;
        try {
            PageInfo<WarehousingVo> pageInfo = warehousingService.advancedSearchByCons(advancedWarehousingVo.getPageNum(),advancedWarehousingVo.getPageSize(),advancedWarehousingVo);
            result = new ResultResponse<>(200,"高级搜索查询入库成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"高级搜索查询入库失败");
        }
        return result;

    }
    /*@PostMapping(value = "/exportExcel")
    @ApiOperation(value = "导出")
    public ResultResponse<Void> exportExcel(HttpServletResponse response , @RequestBody dealSummaryListVo dealSummaryListVo){
        ResultResponse<Void> result = null;
        System.out.println("dsl==============");
        try {
            dealSummaryListVo.printlist();
            List<exportDealSummaryVo> dealSummaryList = dealSummaryListVo.getDealSummaryList();
            lists = dealSummaryList;
            iDealService.getlist(dealSummaryList);

            result = new ResultResponse<>(200,"前台数据传入成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"前台数据传入失败");
        }
        return result;
    }*/
    }

